package com.momo.mal.anime.view;

import java.text.DecimalFormat;

import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.momo.mal.R;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.entity.Anime;
import com.momo.mal.entity.Related;
import com.momo.mal.util.AnimeUtil;

public class AnimeTabInformation extends Fragment {
	private JSONObject jsonObject;
	private String mContent = "???";
	
	private static final String KEY_CONTENT = "AnimeTabInformation:Content";
	
	public static AnimeTabInformation newInstance(JSONObject jsonObject) {
		AnimeTabInformation fragment = new AnimeTabInformation();
		fragment.jsonObject = jsonObject;
		
		return fragment;
	}
	

	@Override
	public void onPause() {
		super.onPause();
		getActivity().getIntent().putExtra("json", jsonObject.toString());
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(getActivity().getIntent().hasExtra("json")) {
			try {
				jsonObject = new JSONObject(getActivity().getIntent().getStringExtra("json"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getActivity().getIntent().hasExtra("json")) {
			try {
				jsonObject = new JSONObject(getActivity().getIntent().getStringExtra("json"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
	}
		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.anime_view_information, null);
		
		MalDatabaseAdapter dbAdapter = new MalDatabaseAdapter(getActivity());
		
		Anime anime = new Anime();
		try {
			if(jsonObject.has("sync") == true) {
				anime = dbAdapter.getAnimeById(jsonObject.getString("anime_id"));
			} else {
				anime = AnimeUtil.parsingAnimeFromJson(jsonObject);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		TextView type = (TextView) view.findViewById(R.id.text_type);
		TextView episode = (TextView) view.findViewById(R.id.text_episode);
		TextView status = (TextView) view.findViewById(R.id.text_status);
		TextView genre = (TextView) view.findViewById(R.id.text_genre);
		TextView classification = (TextView) view.findViewById(R.id.text_classification);
		
		TextView memberScore = (TextView) view.findViewById(R.id.text_member_score);
		TextView rank = (TextView) view.findViewById(R.id.text_rank);
		TextView popularity = (TextView) view.findViewById(R.id.text_popularity);
		TextView memberCount = (TextView) view.findViewById(R.id.text_member_count);
		TextView favorite = (TextView) view.findViewById(R.id.text_favorite);
		
		TextView english = (TextView) view.findViewById(R.id.text_english);
		TextView synonyms = (TextView) view.findViewById(R.id.text_synonyms);
		TextView japanese = (TextView) view.findViewById(R.id.text_japanese);
		
		TextView adaptation = (TextView) view.findViewById(R.id.text_adaptation);
		TextView prequel = (TextView) view.findViewById(R.id.text_prequel);
		TextView sequel = (TextView) view.findViewById(R.id.text_sequel);
		TextView sideStory = (TextView) view.findViewById(R.id.text_side_stories);
		TextView spinOff = (TextView) view.findViewById(R.id.text_spin_off);
		TextView alternative = (TextView) view.findViewById(R.id.text_alternative);
		
		TextView watchedStatus = (TextView) view.findViewById(R.id.text_watched_status);
		TextView watchedEpisode = (TextView) view.findViewById(R.id.text_watched_episode);
		TextView score = (TextView) view.findViewById(R.id.text_score);
		
		TextView titleMyInfo = (TextView) view.findViewById(R.id.title_my_info);
		TextView titleAlternativeTitle = (TextView) view.findViewById(R.id.title_alternative_title);
		TextView titleRelatedAnime = (TextView) view.findViewById(R.id.title_related_anime);
		
		if(anime.getStatusSync() != null) {
			watchedEpisode.setText("Episodes seen : " + anime.getWatchedEpisode() + " / " + anime.getEpisode());
			watchedStatus.setText("Status : " + AnimeUtil.parsingWatchedStatusView(anime.getWathcedStatus()));
			if(anime.getScore().equals("10"))
				score.setText("Score : " + anime.getScore() + " - Masterpiece");
			else if(anime.getScore().equals("9"))
				score.setText("Score : " + anime.getScore() + " - Great");
			else if(anime.getScore().equals("8"))
				score.setText("Score : " + anime.getScore() + " - Very Good");
			else if(anime.getScore().equals("7"))
				score.setText("Score : " + anime.getScore() + " - Good");
			else if(anime.getScore().equals("6"))
				score.setText("Score : " + anime.getScore() + " - Fine");
			else if(anime.getScore().equals("5"))
				score.setText("Score : " + anime.getScore() + " - Average");
			else if(anime.getScore().equals("4"))
				score.setText("Score : " + anime.getScore() + " - Bad");
			else if(anime.getScore().equals("3"))
				score.setText("Score : " + anime.getScore() + " - Very Bad");
			else if(anime.getScore().equals("2"))
				score.setText("Score : " + anime.getScore() + " - Horrible");
			else if(anime.getScore().equals("1"))
				score.setText("Score : " + anime.getScore() + " - Appalling");
			else
				score.setText("Score : -");
		} else {
			titleMyInfo.setVisibility(TextView.GONE);
			watchedEpisode.setVisibility(TextView.GONE);
			watchedStatus.setVisibility(TextView.GONE);
			score.setVisibility(TextView.GONE);
		}
		
		rank.setText("Ranked : #" + anime.getRank());
		popularity.setText("Popularity : #" + anime.getPopularityRank());
		type.setText("Type : " + anime.getType());
		episode.setText("Episode : " + anime.getEpisode());
		status.setText("Status : " + WordUtils.capitalize(anime.getStatus()));
		genre.setText("Genres : " + anime.getGenre());
		classification.setText("Rating : " + anime.getClassification());
		
		DecimalFormat formatterScore = new DecimalFormat("##.00");
		DecimalFormat formatter = new DecimalFormat("#,###");
		memberScore.setText("Score : " + formatterScore.format(Double.parseDouble(anime.getMemberScore())));
		memberCount.setText("Members : " + formatter.format(Double.parseDouble(anime.getMemberCount())));
		favorite.setText("Favorites : " + formatter.format(Double.parseDouble(anime.getFavoriteCount())));
		
		if(anime.getEnglish().length() == 0) {
			english.setVisibility(TextView.GONE);
		}
		if(anime.getSynonyms().length() == 0) {
			synonyms.setVisibility(TextView.GONE);
		}
		if(anime.getJapanese().length() == 0) {
			japanese.setVisibility(TextView.GONE);
		}
		if(english.getVisibility() == TextView.GONE && synonyms.getVisibility() == TextView.GONE && japanese.getVisibility() == TextView.GONE) {
			titleAlternativeTitle.setVisibility(TextView.GONE);
		}
		
		english.setText("English : " + anime.getEnglish());
		japanese.setText("Japanese : " + anime.getJapanese());
		synonyms.setText("Synonyms : " + anime.getSynonyms());
		
		if(anime.getAdaptation().length() == 0) {
			adaptation.setVisibility(TextView.GONE);
		}
		if(anime.getPrequel().length() == 0) {
			prequel.setVisibility(TextView.GONE);
		}
		if(anime.getSequel().length() == 0) {
			sequel.setVisibility(TextView.GONE);
		}
		if(anime.getSideStory().length() == 0) {
			sideStory.setVisibility(TextView.GONE);
		}
		if(anime.getSpinOff().length() == 0) {
			spinOff.setVisibility(TextView.GONE);
		}
		if(anime.getAlternative().length() == 0) {
			alternative.setVisibility(TextView.GONE);
		}
		if(adaptation.getVisibility() == TextView.GONE && 
				prequel.getVisibility() == TextView.GONE && 
				sequel.getVisibility() == TextView.GONE && 
				sideStory.getVisibility() == TextView.GONE && 
				spinOff.getVisibility() == TextView.GONE && 
				alternative.getVisibility() == TextView.GONE) {
			titleRelatedAnime.setVisibility(TextView.GONE);
		}
		
		String adapt = "Adaptation : ", sequels = "Sequel : ", prequels = "Prequel : ", sideStories = "Side Story : ", spinOffs = "Spin-off : ", alternatives = "Alternative : ";
		int intAdapt = 0, intSequels = 0, intPrequels = 0, intSideStories = 0, intSpinOff = 0, intAlternative = 0;
		for(Related r : anime.getRelations()) {
			if(r.getType().equalsIgnoreCase("adaptations")) {
				if(intAdapt == 0) {
					adapt = adapt + "<a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					adapt = adapt + ", <a href=\"manga://view?manga_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intAdapt++;
			}
			
			if(r.getType().equalsIgnoreCase("prequels")) {
				if(intSequels == 0) {
					prequels = prequels + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					prequels = prequels + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intSequels++;
			}
			
			if(r.getType().equalsIgnoreCase("sequels")) {
				if(intPrequels == 0) {
					sequels = sequels + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					sequels = sequels + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intPrequels++;
			}
			
			if(r.getType().equalsIgnoreCase("side_stories")) {
				if(intSideStories == 0) {
					sideStories = sideStories + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					sideStories = sideStories + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intSideStories++;
			}
			
			if(r.getType().equalsIgnoreCase("spin_offs")) {
				if(intSpinOff == 0) {
					spinOffs = spinOffs + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					spinOffs = spinOffs + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intSpinOff++;
			}
			
			if(r.getType().equalsIgnoreCase("alternative_versions")) {
				if(intAlternative == 0) {
					alternatives = alternatives + "<a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				} else {
					alternatives = alternatives + ", <a href=\"anime://view?anime_id=" + r.getRelatedId() + "&title=" + r.getTitle() + "\">" + r.getTitle() + "</a>";
				}
				
				intAlternative++;
			}
			
		}
		
		adaptation.setText(Html.fromHtml(adapt.toString()));
		adaptation.setMovementMethod(LinkMovementMethod.getInstance());
		
		prequel.setText(Html.fromHtml(prequels.toString()));
		prequel.setMovementMethod(LinkMovementMethod.getInstance());
		
		sequel.setText(Html.fromHtml(sequels.toString()));
		sequel.setMovementMethod(LinkMovementMethod.getInstance());
		
		sideStory.setText(Html.fromHtml(sideStories.toString()));
		sideStory.setMovementMethod(LinkMovementMethod.getInstance());
		
		spinOff.setText(Html.fromHtml(spinOffs.toString()));
		spinOff.setMovementMethod(LinkMovementMethod.getInstance());
		
		alternative.setText(Html.fromHtml(alternatives.toString()));
		alternative.setMovementMethod(LinkMovementMethod.getInstance());
		
		return view;
	}
	
}
